#!/usr/bin/python

import gobject;
gobject.threads_init()
import gst;

if __name__ == "__main__":
    # First create our pipeline
    pipe = gst.Pipeline("mypipe")

    # Create a software mixer with "Adder"
    adder = gst.element_factory_make("adder","audiomixer")
    

    #Create microphone
    microphone = gst.element_factory_make("pulsesrc","microphone")
    microphone.set_property("device","alsa_input.pci-0000_02_02.0.analog-stereo")
    mic_volume = gst.element_factory_make("volume","mic_volume")
    mic_volume.set_property("volume", 1)
    mic_audioconvert = gst.element_factory_make("audioconvert","mic_audioconvert")

    pipe.add(microphone, mic_volume, mic_audioconvert, adder)
    
    #Link the microphone to the mixer
    gst.element_link_many(microphone, mic_volume, mic_audioconvert, adder)
    
    #Create the music 
    source = gst.element_factory_make("filesrc", "file-source")
    source.set_property("location", "/home/rado/prj/proto/mic.ogg")
    decoder = gst.element_factory_make("oggdemux", "oggdemux")
    songaudioconvert = gst.element_factory_make("vorbisdec", "vorbisdec")
    volume_control = gst.element_factory_make("volume", "volume_control")
    volume_control.set_property("volume", 0.1)
    
    pipe.add(source, decoder, songaudioconvert, volume_control)
    
    #Link the music to the mixer
    gst.element_link_many(source, decoder, songaudioconvert, volume_control, adder)
    
    
    #Create splitter which will split the output  - for stream and output
    splitter = gst.element_factory_make("tee", "splitter")
    pipe.add(splitter)
    adder.link(splitter)
    
    
    #Create output
    output = gst.element_factory_make("pulsesink", "audio_out")
    audioconvert = gst.element_factory_make("audioconvert","audioconvert")
    
    directoutput = gst.element_factory_make("queue", "directoutput")
    pipe.add(directoutput)
    
    pipe.add(audioconvert, output)
    
    #Link adder to the output
    #gst.element_link_many(adder, audioconvert, output)
    gst.element_link_many(splitter, directoutput, audioconvert, output)
    
    #Create streamer
    vor = gst.element_factory_make("vorbisenc", "vorbisenc")
    oggmux = gst.element_factory_make("oggmux", "oggmux")
    shout2send = gst.element_factory_make("shout2send", "shout2send")
    
    shout2send.set_property("mount", "/stream.ogg")
    shout2send.set_property("port", 8000)
    shout2send.set_property("ip", "192.168.1.110")
    shout2send.set_property("password", "hackme")
    pipe.add(vor, oggmux, shout2send)
    #gst.element_link_many(adder, vor, oggmux, shout2send)
    gst.element_link_many(splitter, vor, oggmux, shout2send)

    # Start the playback
    pipe.set_state(gst.STATE_PLAYING)
    
    
    
    raw_input("Press <ENTER> to change song.")
    
    
    source.unlink(decoder)
    source2 = gst.element_factory_make("filesrc", "file-source2")
    source2.set_property("location", "/home/rado/prj/proto/mic.ogg")
    pipe.add(source2)
    source2.link(decoder)
    
    volume_control.set_property("volume", 0.3)
    
    
    pipe.set_state(gst.STATE_PLAYING)
    
    
    
    
    
    
    raw_input("Press <ENTER> to EXIT.")
    
    
    
    
    
    